package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterEt;
import com.atguigu.eduservice.entity.chapter.VideoEt;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-26
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService; //注入小节的service

    //课程大纲列表，根据课程id进行查询
    @Override
    public List<ChapterEt> getChapterVideoByCourseId(String courseId) {

        //1 根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //2 根据课程id查询课程里面的所有的小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);

        //创建list集合，用于最终封装数据
        List<ChapterEt> finalList = new ArrayList<>();

        //3 遍历查询章节list集合进行封装
        for (EduChapter item : eduChapterList) {
            ChapterEt chapterEt = new ChapterEt();
            BeanUtils.copyProperties(item, chapterEt);

            //创建集合，用于封装章节的小节
            List<VideoEt> videoEtList = new ArrayList<>();
            //4 遍历查询小节list集合，进行封装
            for (EduVideo item2 : eduVideoList) {
                if (item2.getChapterId().equals(item.getId())) {
                    //进行封装
                    VideoEt videoEt = new VideoEt();
                    BeanUtils.copyProperties(item2, videoEt);
                    //放到用于封装小节的集合中
                    videoEtList.add(videoEt);
                }
            }
            //把封装之后的小节list集合放到章节对象里面
            chapterEt.setChildren(videoEtList);

            //把chapterEt放到最终List集合
            finalList.add(chapterEt);
        }


        return finalList;
    }

    //删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapterId章节id查询小节表，如果有数据，就不进行删除。
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = videoService.count(wrapper);
        //判断
        if (count > 0) {
            throw new GuliException(20001, "不能删除");
        } else {
            int result = baseMapper.deleteById(chapterId);
            return result > 0;
        }
    }

    //2 根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
